<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nginx 负载均衡会话保持技术详解</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e293b;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: #334155;
            padding: 8px 16px;
            font-size: 14px;
            color: #94a3b8;
        }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            color: #e2e8f0;
            line-height: 1.6;
        }
        .section-number {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 700;
            font-size: 3rem;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Nginx 负载均衡<br>
                    <span class="text-purple-200">会话保持技术详解</span>
                </h1>
                <p class="text-xl md:text-2xl text-purple-100 max-w-3xl mx-auto leading-relaxed">
                    深入探索 Nginx 中的会话保持机制，确保用户请求的连续性与一致性，打造无缝的用户体验
                </p>
                <div class="mt-10 flex justify-center space-x-6">
                    <div class="text-center">
                        <i class="fas fa-server text-4xl mb-2"></i>
                        <p class="text-sm">负载均衡</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-link text-4xl mb-2"></i>
                        <p class="text-sm">会话保持</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-shield-alt text-4xl mb-2"></i>
                        <p class="text-sm">高可用性</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Introduction Section -->
    <section class="py-16 px-6">
        <div class="max-w-4xl mx-auto">
            <div class="bg-white rounded-2xl shadow-xl p-10">
                <h2 class="text-3xl font-bold text-gray-800 mb-6 flex items-center">
                    <i class="fas fa-info-circle text-purple-600 mr-3"></i>
                    什么是会话保持？
                </h2>
                <p class="text-lg text-gray-700 leading-relaxed indent-8">
                    在 Nginx 中，负载均衡器通过多种方式来处理会话保持。会话保持（Session Persistence）是确保用户的后续请求被路由到相同的后端服务器，从而保持会话状态的一种技术。Nginx 提供了几种机制来实现会话保持，让我们深入了解每一种方法。
                </p>
            </div>
        </div>
    </section>

    <!-- Visualization Section -->
    <section class="py-8 px-6">
        <div class="max-w-6xl mx-auto">
            <div class="bg-white rounded-2xl shadow-xl p-10">
                <h2 class="text-3xl font-bold text-gray-800 mb-8 text-center">
                    <i class="fas fa-sitemap text-purple-600 mr-3"></i>
                    会话保持技术架构图
                </h2>
                <div class="mermaid">
                    graph TB
                        A[客户端请求] --> B{Nginx 负载均衡器}
                        B --> C[会话保持策略]
                        C --> D[IP Hash]
                        C --> E[Cookie Based]
                        C --> F[Hash Based]
                        D --> G[后端服务器1]
                        D --> H[后端服务器2]
                        E --> G
                        E --> H
                        F --> G
                        F --> H
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#f59e0b,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
        </div>
    </section>

    <!-- Methods Section -->
    <section class="py-16 px-6">
        <div class="max-w-6xl mx-auto">
            <h2 class="text-4xl font-bold text-center text-gray-800 mb-12">
                三种会话保持实现方式
            </h2>
            
            <!-- Method 1: IP Hash -->
            <div class="mb-12 card-hover">
                <div class="bg-white rounded-2xl shadow-lg overflow-hidden">
                    <div class="flex">
                        <div class="w-24 bg-gradient-to-b from-purple-600 to-purple-700 flex items-center justify-center">
                            <span class="section-number">1</span>
                        </div>
                        <div class="flex-1 p-8">
                            <h3 class="text-2xl font-bold text-gray-800 mb-4 flex items-center">
                                <i class="fas fa-network-wired text-purple-600 mr-3"></i>
                                基于 IP 地址的会话保持
                            </h3>
                            <p class="text-gray-700 mb-6 leading-relaxed">
                                通过 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ip_hash</code> 指令，Nginx 将客户端的 IP 地址哈希到特定的后端服务器。这种方法确保来自同一 IP 地址的请求始终路由到相同的后端服务器。
                            </p>
                            <div class="code-block">
                                <div class="code-header">
                                    <i class="fas fa-code mr-2"></i>nginx 配置示例
                                </div>
                                <pre><code>upstream backend {
    ip_hash;  # 启用基于 IP 的会话保持
    server backend1.demo.com;
    server backend2.demo.com;
}

server {
    listen 80;
    location / {
        proxy_pass http://backend;
    }
}</code></pre>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Method 2: Cookie Based -->
            <div class="mb-12 card-hover">
                <div class="bg-white rounded-2xl shadow-lg overflow-hidden">
                    <div class="flex">
                        <div class="w-24 bg-gradient-to-b from-purple-600 to-purple-700 flex items-center justify-center">
                            <span class="section-number">2</span>
                        </div>
                        <div class="flex-1 p-8">
                            <h3 class="text-2xl font-bold text-gray-800 mb-4 flex items-center">
                                <i class="fas fa-cookie-bite text-purple-600 mr-3"></i>
                                基于 Cookie 的会话保持
                            </h3>
                            <p class="text-gray-700 mb-6 leading-relaxed">
                                使用 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">sticky</code> 模块来通过 Cookie 实现会话保持。<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">sticky</code> 模块允许将一个唯一的 Cookie 分配给每个客户端，客户端的后续请求会被路由到设置了该 Cookie 的服务器。
                            </p>
                            <div class="code-block">
                                <div class="code-header">
                                    <i class="fas fa-code mr-2"></i>nginx 配置示例
                                </div>
                                <pre><code>upstream backend {
    # 配置 sticky 模块来实现基于 Cookie 的会话保持
    sticky cookie srv_id expires=1h path=/;
    server backend1.demo.com;
    server backend2.demo.com;
}

server {
    listen 80;
    location / {
        proxy_pass http://backend;
    }
}</code></pre>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Method 3: Hash Based -->
            <div class="mb-12 card-hover">
                <div class="bg-white rounded-2xl shadow-lg overflow-hidden">
                    <div class="flex">
                        <div class="w-24 bg-gradient-to-b from-purple-600 to-purple-700 flex items-center justify-center">
                            <span class="section-number">3</span>
                        </div>
                        <div class="flex-1 p-8">
                            <h3 class="text-2xl font-bold text-gray-800 mb-4 flex items-center">
                                <i class="fas fa-hashtag text-purple-600 mr-3"></i>
                                基于 Hash 的会话保持
                            </h3>
                            <p class="text-gray-700 mb-6 leading-relaxed">
                                通过 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">hash</code> 指令，Nginx 可以使用请求的某些部分（如 Cookie 或请求头）来决定将请求路由到哪个后端服务器。这种方法可以确保具有相同哈希值的请求始终路由到同一服务器。
                            </p>
                            <div class="code-block">
                                <div class="code-header">
                                    <i class="fas fa-code mr-2"></i>nginx 配置示例
                                </div>
                                <pre><code>upstream backend {
    hash $cookie_session_id consistent;  # 基于 Cookie 中的 session_id 进行哈希
    server backend1.demo.com;
    server backend2.demo.com;
}

server {
    listen 80;
    location / {
        proxy_pass http://backend;
    }
}</code></pre>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Comparison Section -->
    <section class="py-16 px-6 bg-gray-100">
        <div class="max-w-6xl mx-auto">
            <h2 class="text-4xl font-bold text-center text-gray-800 mb-12">
                <i class="fas fa-balance-scale text-purple-600 mr-3"></i>
                方案对比与选择指南
            </h2>
            <div class="grid md:grid-cols-3 gap-8">
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="text-center mb-4">
                        <i class="fas fa-network-wired text-4xl text-purple-600"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800 mb-3 text-center">IP Hash</h3>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                            <span>配置简单，开箱即用</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                            <span>无需客户端支持</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-times text-red-500 mt-1 mr-2"></i>
                            <span>NAT环境下可能失效</span>
                        </li>
                    </ul>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="text-center mb-4">
                        <i class="fas fa-cookie-bite text-4xl text-purple-600"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800 mb-3 text-center">Cookie Based</h3>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                            <span>精确的会话控制</span>
                        </li>